package com.edu.robot.modules.robot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.robot.model.R;
import com.edu.robot.model.enums.ResultCodeEnum;
import com.edu.robot.model.error.BizException;
import com.edu.robot.modules.robot.controller.vo.PageQuery;
import com.edu.robot.modules.robot.controller.vo.QueryRobotVO;
import com.edu.robot.modules.robot.controller.vo.RobotVO;
import com.edu.robot.modules.robot.corvert.RobotCovert;
import com.edu.robot.modules.robot.dto.PageResult;
import com.edu.robot.modules.robot.dto.RobotInfoDTO;
import com.edu.robot.modules.robot.mapper.CrRobotMapper;
import com.edu.robot.modules.robot.model.CrRobot;
import com.edu.robot.modules.robot.service.CrModelConfigService;
import com.edu.robot.modules.robot.service.CrPromptService;
import com.edu.robot.modules.robot.service.CrRobotService;
import com.edu.robot.modules.robot.service.CrToolService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* @author 86177
* @description 针对表【cr_robot】的数据库操作Service实现
* @createDate 2025-02-22 14:34:38
*/
@Service
@Slf4j
public class CrRobotServiceImpl extends ServiceImpl<CrRobotMapper, CrRobot>
    implements CrRobotService {

	@Autowired
	private CrToolService crToolService;

	@Autowired
	private CrPromptService crPromptService;

	@Autowired
	private CrModelConfigService crModelConfigService;

	@Override
	public R<Boolean> createRobot(RobotVO robotVO) {

		log.info("CrRobotServiceImpl createRobot robotVo info:{}", robotVO);

		//1.校验参数
		if(Objects.isNull(robotVO)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		if(Objects.isNull(robotVO.getModelId()) || Objects.isNull(robotVO.getPromptId())){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		//2.保存机器人
		CrRobot crRobot = RobotCovert.convert(robotVO);

		return R.success(save(crRobot));
	}

	@Override
	public R<RobotInfoDTO> getRobot(Long id) {

		if (Objects.isNull(id)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		//1.查询机器人
		CrRobot crRobot = getById(id);

		if (Objects.isNull(crRobot)){
			throw new BizException(ResultCodeEnum.DATA_NOT_FOUND,"机器人不存在");
		}

		RobotInfoDTO convert = RobotCovert.convert(crRobot);

		return R.success(convert);
	}

	@Override
	public PageResult<RobotInfoDTO> queryRobotList(PageQuery<QueryRobotVO> pageQuery) {

		if (Objects.isNull(pageQuery)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		LambdaQueryWrapper<CrRobot> queryWrapper = new LambdaQueryWrapper<>();

		Optional.ofNullable(pageQuery.getQuery()).ifPresent(query -> {
			if (StringUtils.isNotBlank(query.getRobotName())){
				queryWrapper.like(CrRobot::getName,query.getRobotName());
			}

			if (StringUtils.isNotBlank(query.getStatus())){
				queryWrapper.eq(CrRobot::getStatus,query.getStatus());
			}

			if (StringUtils.isNotBlank(query.getRobotId())){
				queryWrapper.eq(CrRobot::getId,query.getRobotId());
			}

			if(StringUtils.isNotBlank(query.getRobotCreator())){
				queryWrapper.eq(CrRobot::getCreatedBy,query.getRobotCreator());
			}

			if (StringUtils.isNotBlank(query.getStatus())){
				queryWrapper.eq(CrRobot::getStatus,query.getStatus());
			}

		});


		// 设置排序
		queryWrapper.orderByDesc(CrRobot::getCreatedAt);

		//默认每页10条数据
		Long size = Optional.ofNullable(pageQuery.getPageSize()).orElse(10L);

		//默认第1页
		Long current = Optional.ofNullable(pageQuery.getPageNum()).orElse(1L);

		// 查询分页数据
		IPage<CrRobot> pageParam = new Page<>(current, size);
		IPage<CrRobot> pageResult = page(pageParam, queryWrapper);

		if (Objects.isNull(pageResult)){
			throw new BizException(ResultCodeEnum.DATABASE_ERROR,"数据库查询失败");
		}

		// 构建结果集
		PageResult<RobotInfoDTO> res = new PageResult<>();
		res.setTotal(pageResult.getTotal());
		res.setCurrentPage(pageResult.getCurrent());
		res.setTotalPages(pageResult.getPages());

		if(pageResult.getRecords().isEmpty()){
			return res;
		}

		List<RobotInfoDTO> list = pageResult.getRecords().stream().map(RobotCovert::convert).toList();

		res.setRecords(list);

		return res;
	}

	@Override
	public R<Boolean> updateRobot(RobotVO robotVO) {

		if (Objects.isNull(robotVO)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		if (Objects.isNull(robotVO.getId())){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		if (Objects.isNull(robotVO.getModelId()) || Objects.isNull(robotVO.getPromptId())){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		CrRobot robotCovert = RobotCovert.convert(robotVO);

		if (updateById(robotCovert)){
			return R.success();
		}


		return R.fail("更新机器人异常");
	}
}




